
Namespace Validation
    <AttributeUsage(AttributeTargets.Property, AllowMultiple:=False, Inherited:=False)> Public NotInheritable Class StringLengthValidatorAttribute
        Inherits BaseValidatorAttribute

#Region " Declarations "

        Private _enumAllowNullString As AllowNullString = Validation.AllowNullString.No
        Private _intMaximumLength As Integer = -1
        Private _intMinimumLength As Integer = -1

#End Region

#Region " Properties "

        Public ReadOnly Property AllowNullString() As AllowNullString
            Get
                Return _enumAllowNullString
            End Get
        End Property

        Public ReadOnly Property MaximumLength() As Integer
            Get
                Return _intMaximumLength
            End Get
        End Property

        Public ReadOnly Property MinimumLength() As Integer
            Get
                Return _intMinimumLength
            End Get
        End Property

#End Region

#Region " Constructor "

        Public Sub New(ByVal intMaximumLength As Integer)
            Me.New(-1, intMaximumLength, Validation.AllowNullString.No)
        End Sub

        Public Sub New(ByVal intMinimumLength As Integer, ByVal intMaximumLength As Integer)
            Me.New(intMinimumLength, intMaximumLength, Validation.AllowNullString.No)
        End Sub

        Public Sub New(ByVal intMinimumLength As Integer, ByVal intMaximumLength As Integer, ByVal enumAllowNullString As AllowNullString)

            If intMaximumLength < 1 Then
                Throw New ArgumentOutOfRangeException("intMaximumLength", "MaximumLength must be greater than 0.")

            ElseIf intMaximumLength < intMinimumLength Then
                Throw New ArgumentOutOfRangeException("intMaximumLength", "MaximumLength must be greater than or equal to the MinimumLength.")
            End If

            _intMinimumLength = intMinimumLength
            _intMaximumLength = intMaximumLength
            _enumAllowNullString = enumAllowNullString
        End Sub

        Public Sub New(ByVal intMaximumLength As Integer, ByVal enumAllowNullString As AllowNullString)
            Me.New(-1, intMaximumLength, enumAllowNullString)
        End Sub

#End Region

#Region " Methods "

        Public Overrides Function Create(ByVal strPropertyName As String) As Validator
            Return New Validator(AddressOf Validation.StringValidationRules.StringLengthRule, New StringLengthRuleEventArgs(Me, strPropertyName), RuleType.Attribute)
        End Function

#End Region

    End Class

End Namespace
